<?php
include_once('./config/config.php');
$admin = AuthCheck();
$action = $_GET['action'] ?? '';
// 退出账号
if ($action === 'logout') {
    setcookie('auth', '', time() - 1);
    notice('退出成功', 'login.php');
}

// 当前页
$page = $_GET['page'] ?? 1;
$CountAdmin = "SELECT COUNT(*) AS `c` FROM `pre_admin`";
$count = find($CountAdmin)['c'];  // 
$limit = 5; // 限制查询和显示
$start = ($page - 1) * $limit; // 从第几条数据开始查找
$AdminSql = "SELECT * FROM `pre_admin` ORDER BY `id` LIMIT $start,$limit";
$AdminData = all($AdminSql); // 查询数据用于渲染
$ShowPage = ShowPage($page, $count, $limit); // 分页器

// 不允许将所有管理员都删除
$countSql = "SELECT COUNT(*) AS `c` FROM `pre_admin`";
$count = find($countSql)['c'];
?>

<!DOCTYPE html>
<html lang="en">

<head>
    <!-- 公共样式 -->
    <?php include_once('./common/meta.php'); ?>

    <!-- Le fav and touch icons -->
    <link rel="shortcut icon" href="../assets/ico/favicon.ico">
    <link rel="apple-touch-icon-precomposed" sizes="144x144" href="../assets/ico/apple-touch-icon-144-precomposed.png">
    <link rel="apple-touch-icon-precomposed" sizes="114x114" href="../assets/ico/apple-touch-icon-114-precomposed.png">
    <link rel="apple-touch-icon-precomposed" sizes="72x72" href="../assets/ico/apple-touch-icon-72-precomposed.png">
    <link rel="apple-touch-icon-precomposed" href="../assets/ico/apple-touch-icon-57-precomposed.png">
</head>

<body>

    <!-- 公共头部 -->
    <?php include_once('./common/header.php') ?>

    <!-- 侧边菜单栏 -->
    <?php include_once('./common/menu.php') ?>

    <div class="content">
        <div class="header">
            <h1 class="page-title">管理员列表</h1>
        </div>
        <ul class="breadcrumb">
            <li><a href="index.php">Home</a> <span class="divider">/</span></li>
            <li class="active">AdminList</li>
        </ul>

        <div class="container-fluid">
            <div class="row-fluid">
                <div class="btn-toolbar">
                    <button class="btn btn-primary" onClick="location='AdminAdd.php'"><i class="icon-plus"></i>添加管理员</button>
                </div>
                <div class="well">
                    <table class="table">
                        <thead>
                            <tr>
                                <th>ID</th>
                                <th>头像</th>
                                <th>管理员名称</th>
                                <th style="width: 30px;">操作</th>
                            </tr>
                        </thead>
                        <tbody>

                            <?php foreach ($AdminData as $item) { ?>
                                <tr>
                                    <td><?php echo $item['id']; ?></td>
                                    <td>
                                        <a href="<?php echo is_file('.' . $item['avatar']) ? $item['avatar'] : '/assets/images/avatar.png'; ?>" target="_blank">
                                            <img src="<?php echo is_file('.' . $item['avatar']) ? $item['avatar'] : '/assets/images/avatar.png'; ?>" style="width: 25px;height:25px;border-radius:50%;object-fit: cover;" alt="">
                                        </a>
                                    </td>
                                    <td><?php echo $item['username']; ?></td>
                                    <td>
                                        <a href="AdminEdit.php?id=<?php echo $item['id']; ?>"><i class="icon-pencil"></i></a>
                                        <a href="#myModal" role="button" data-toggle="modal"><i class="icon-remove" data-id="<?php echo $item['id'] ?>"></i></a>
                                    </td>
                                </tr>
                            <?php } ?>

                        </tbody>
                    </table>
                </div>
                <div class="pagination">
                    <?php echo $ShowPage; ?>
                </div>

                <div class="modal small hide fade" id="myModal" tabindex="-1" role="dialog"
                    aria-labelledby="myModalLabel" aria-hidden="true">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                        <h3 id="myModalLabel">删除</h3>
                    </div>
                    <div class="modal-body">
                        <p class="error-text"><i class="icon-warning-sign modal-icon"></i>是否确认删除该管理员</p>
                    </div>
                    <div class="modal-footer">
                        <button class="btn" data-dismiss="modal" aria-hidden="true">取消</button>
                        <button class="btn btn-danger" data-dismiss="modal">确认</button>
                    </div>
                </div>
                <!-- 公共底部 -->
                <?php include_once('./common/footer.php') ?>
            </div>
        </div>
    </div>
</body>

</html>
<!-- 公共js -->
<?php include_once('./common/script.php') ?>
<script>
    let count = <?php echo $count; ?> ?? 0;
    // 删除功能
    // 获取要被删除的admin的id
    let id = 0
    $('.icon-remove').on('click', function() {
        id = $(this).data('id')
    })
    // 点击确认
    $('.btn-danger').click(() => {
        if (count <= 1) {
            alert('不允许删除最后一条数据')
            return
        }
        // 发请求
        $.ajax({
            url: 'api.php',
            type: 'post',
            data: {
                id,
                action: 'delete_admin'
            },
            dataType: 'json',
            success: res => {
                if (res.code === 1) {
                    alert(res.msg)
                    location.reload()
                } else alert(res.msg)
            }
        })
    })
</script>